class my_deque():
    def __init__(self):
        self.data=[None]*5
        self.head=0
        self.tail=0 #超出末端
        self.is_empty=True
        self.is_full=False
    def append_right(self,x):
        if self.is_full:
            raise Exception('Deque Overflow')
        self.data[self.tail]=x
        if self.tail==len(self.data)-1:
            self.tail=0
        else:
            self.tail+=1
        self.is_empty=False
        if self.tail==self.head:
            self.is_full=True
    def append_left(self,x):
        if self.is_full:
            raise Exception('Deque Overflow')
        if self.head==0:
            self.head=len(self.data)-1
        else:
            self.head-=1
        self.data[self.head]=x
        self.is_empty=False
        if self.tail==self.head:
            self.is_full=True
    def pop_left(self):
        if self.is_empty:
            raise Exception('Deque Unerflow')
        x=self.data[self.head]
        if self.head==len(self.data)-1:
            self.head=0
        else:
            self.head+=1
        self.is_full=False
        if self.head==self.tail:
            self.is_empty=True
        return x
    def pop_right(self):
        if self.is_empty:
            raise Exception('Deque Underflow')
        if self.tail==0:
            self.tail=len(self.data)-1
        else:
            self.tail-=1
        x=self.data[self.tail]
        self.is_full=False
        if self.head==self.tail:
            self.is_empty=True
        return x

if __name__=='__main__':
    a=my_deque()
    a.append_left(1)
    print a.head
    print a.tail
    a.append_left(2)
    a.append_left(100)
    a.append_left(10)
    a.append_left(2)
    #a.append_left(5)
    print a.head
    print a.tail
    print a.data
    print a.pop_right()
    print a.pop_right()
    print a.pop_right()
    print a.pop_right()
    print a.pop_right()
    #print a.pop_right()
